<?php
/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework										|
 * | Version 1.0																|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>					|
 * | Blog: http://mcezzare.blogspot.com.br										|
 * |																			|
 * | This program is part of PHPKiisf, and is free software: you can			|
 * | redistribute it and/or modify it under the terms of the GNU 				|
 * | General Public License as published by										|
 * | the Free Software Foundation, either version 3 of the License, or			|
 * | (at your option) any later version.										|
 * |																			|
 * | This program is distributed in the hope that it will be useful,			|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of				|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the				|
 * | GNU General Public License for more details.								|
 * | You should have received a copy of the GNU General Public License			|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.		|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>			|
 * +----------------------------------------------------------------------------+
 * | Contributors																|
 * |																			|
 * +----------------------------------------------------------------------------+
 *
 */
/**
 * This file is the view used for build MVC objects
 *
 * @package Templates
 * @access public
 * @since 2013-03-08
 * @author Mario Cezzare <mcezzare@gmail.com>
 */
require_once sprintf( "%s/%s%s" , base_template::$public_dir , 'header' , INCLUDE_EXTENSION ) ;
require_once ( "syntax_highlighter_headers.inc.php" );
require_once sprintf( "%s/%s%s" , base_template::$public_dir , 'navbar' , INCLUDE_EXTENSION ) ;


//just to minify
$global_params = base_template::$global_params ;
/**
 * Verifiy if this page is running on test mode
 */
$test_mode = false ;
if ( is_array( $global_params ) ) {
	if ( in_array( "test" , $global_params[0] ) ) {
		$test_mode = true ;
	}
}
$dao = new default_dao() ;

//uncomment for debug
//echo __FILE__ . "<br>" ;
//print_r( dispatcher::$_active_route ) ;
//print_r( $global_params ) ;
?>


<div class="container">
    <div class="hero-unit">
		<h1>
			<?php print base_template::$section_name ; ?>

		</h1>
		<p>-----</p>

		<!--<p><a class="btn btn-primary btn-large">Learn more &raquo;</a></p>-->
    </div>
    <div class="container">
		<!--form-inline container-fluid-->
		<div class="accordion-heading">
			<ul class="nav nav-tabs nav-stacked">
				<li><a href="#model">1 - Generate Models from Database</a></li>
				<!--								<li><a href="#database">2 - Configuring Database Access</a></li>
												<li><a href="#mvc_gen">3 - Generating Php Code for Bussiness Objects</a></li>-->
			</ul>

			<p>&nbsp;</p>

		</div>
		<?php /* start tables builder form */ ?>
		<div class="container-fluid">
			<h4>Tables</h4>
			<?php
			$div_name = 'model' ;
			gui_controller::create_button( array(
				"id" => sprintf( "bt_view_%s" , $div_name ) ,
				"onclick" => "phpkiisf_js.display_box(this,$('#div_$div_name'));" ,
				"class" => "btn btn-small" ,
				"value" => "Hide Details &raquo;" ,
			) ) ;
			?>
			<div id="<?php printf( "div_%s" , $div_name ) ; ?>" class="simple_border">

				<?php
				$array = $dao->get_tables( $indexes_as_text = true ) ; // return array with text indexes
				if ( !$array ) {
					utils::inform_user( new message( "DB_ACCESS_TABLE" , "DB_ACCESS" , "You did not created any tables on Database Yet" , "Create a table first" ) ) ;
				}
				/* The form properties */
				$form_properties = array(
					"id" => "form_$div_name" ,
					"name" => "form_$div_name" ,
					"method" => "post" ,
					"action" => "/" . dispatcher::$_active_route ,
						) ;
				/* this is the form button for post the selected model form */
				$simple_model_button = array(
					"index" => 1 ,
					"object" => "button" ,
					"id" => sprintf( "bt_test_%s" , $div_name ) ,
					"value" => "Create $div_name" ,
					"href" => "#" ,
					"class" => "btn btn-primary" ,
					"onclick" => "phpkiisf_js.post_form($('#form_$div_name'));" ,
						) ;
				$simple_select = array(
					"index" => 0 ,
					"object" => "select" ,
					"id" => "mysql_tables" ,
					"name" => "mysql_tables" ,
					"content" => $array ,
					"selected" => read_var( "mysql_tables" , "P" ) ,
						) ;
				/* Put all Form fields in an array */
				$fields = array(
					$simple_select ,
					$simple_model_button ,
						) ;

				/* Render the form  */
				gui_controller::create_form( $form_properties , $fields ) ;
				?>
			</div>
		</div>
		<?php /* start process tables */ ?>
		<div class="container-fluid">
			<?php
			$table_name = read_var( "mysql_tables" , "P" ) ;
			/* in php5.3 this test is false only Post is null  */
			if ( $table_name ) {
				printf( "<h2>Dumping table :%s</h2>" , $table_name ) ;
				?>
			</div>
			<div class="accordion" id="accordion2">
				<div class="accordion-group">
					<div class="accordion-heading">
						<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
							Table Properties
						</a>
					</div>
					<div id="collapseOne" class="accordion-body collapse in">
						<div class="accordion-inner" style="overflow-style: auto">
							<?php
							print gui_controller::create_table( $dao->get_table_properties( $table_name ) , $print = true , $vertical = false ) ;
							//														print utils::show_object( $dao->get_table_properties( $table_name ) );
							?>
						</div>
					</div>
				</div>
				<div class="accordion-group">
					<div class="accordion-heading">
						<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
							Table Columns
						</a>
					</div>
					<div id="collapseTwo" class="accordion-body collapse">
						<div class="accordion-inner">
							<?php print utils::show_object( $dao->get_columns_from_table( $table_name ) ) ; ?>
						</div>
					</div>
				</div>
				<div class="accordion-group">
					<div class="accordion-heading">
						<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThree">
							Table Indexes & Keys
						</a>
					</div>
					<div id="collapseThree" class="accordion-body collapse">
						<div class="accordion-inner">
							<?php print utils::show_object( $dao->get_table_indexes( $table_name ) ) ; ?>
						</div>
					</div>
				</div>
				<div class="accordion-group">
					<div class="accordion-heading">
						<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseFour">
							Base Model
						</a>
					</div>
					<div id="collapseFour" class="accordion-body collapse">
						<div class="accordion-inner">
							<?php
							$def_model = new default_model() ;
							$def_model->set_init_table_name( $table_name ) ;
							utils::show_object( $def_model , "" , "" , "pre_base_model" ) ;
							?>
						</div>
					</div>
				</div>
				<div class="accordion-group">
					<div class="accordion-heading">
						<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseFive" style="display: block;">
							This Model
						</a>
					</div>
					<div id="collapseFive" class="accordion-body collapse">
						<div class="accordion-inner">
							<?php
							$output = file_get_contents( TEMPLATES_DIR . "/phpmodels/template_default_model.inc.txt" ) ;
							?>
							<pre id="copy_n_paste" class="">
							<?php
								$cleanup_object = $def_model->scaffold() ;
								printf(  $output , date('Y-m-d h:i:s'), $table_name , $table_name ,$table_name) ;
							?>
							</pre>
						</div>

					</div>
				</div>
			</div>
		<?php }
		?>
		<div class="span12">

			<hr>
			<footer>
				<p>
					&copy; <a href="<?php print FMW_URL_ABOUT ; ?>" target="_blank">PHPKissf 2013</a>
				</p>
			</footer>
		</div>
    </div>
</div>
<script language="javascript">
	$(document).ready(function() {
		$("#mysql_tables").val('<?php print($table_name ) ; ?>');
		/* hide initial div*/
		$("a[href='#collapseOne']").click();
		// show final model
		$("a[href='#collapseFive']").click();
		
		/**
		 * Quick and dirty Way of display the final code ready to copy and 
		 * paste on new model :) 
		 * todo: doit in a better way if possible ;) 
		 */
		$("#final_model").empty().append($("#pre_base_model").text());
		var bkp = $("#final_model").text();
		$("#final_model").empty().append(bkp);
		$("#pre_base_model").addClass("brush :php");
		$("#final_model").addClass("brush :php") ;
		var bkp2 = $("#copy_n_paste").text();
//		bkp2.substr(3);
		$("#copy_n_paste").empty();
		$("#copy_n_paste").append("&lt;?php");
		$("#copy_n_paste").append(bkp2);
		$("#copy_n_paste").addClass("brush :php") ;	
		/**
		 * End dirty Job
		 */
		

		
	});
</script>
<!-- /container -->
<?php
require_once sprintf( "%s/%s%s" , base_template::$public_dir , 'footer' , INCLUDE_EXTENSION ) ;
?>
